package com.hy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.Result.ResponseResult;
import com.hy.mapper.*;
import com.hy.pojo.doctor.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppDoctorServiceImpl implements AppDoctorService{
    @Autowired
    private AppDoctorMapper appDoctorMapper;
    @Autowired
    private AppBigdepMapper appBigdepMapper;
    @Autowired
    private AppHospitallMapper appHospitallMapper;
    @Autowired
    private AppDoctorInfoMapper appDoctorInfoMapper;

    @Override
    public ResponseResult list() {
        List<AppDoctor> appdoctors = appDoctorMapper.selectList(null);
        List<DoctorAndBigdepAndHostpital>doctorAndBigdepAndHostpitals=new ArrayList<>();
        for (AppDoctor appDoctor : appdoctors) {
            LambdaQueryWrapper<AppBigdep> lm = new LambdaQueryWrapper<>();
            lm.eq(AppBigdep::getId, appDoctor.getDepartmentId());
            List<AppBigdep> appBigdeps = appBigdepMapper.selectList(lm);
            LambdaQueryWrapper<APPHospital>lms=new LambdaQueryWrapper<>();
            lms.eq(APPHospital::getId,appDoctor.getHospitalId());
            List<APPHospital> appHospitals = appHospitallMapper.selectList(lms);
            DoctorAndBigdepAndHostpital doctorAndBigdepAndHostpital = new DoctorAndBigdepAndHostpital(appDoctor, appBigdeps.get(0), appHospitals.get(0));
            doctorAndBigdepAndHostpitals.add(doctorAndBigdepAndHostpital);
        }
        return ResponseResult.okResult(200,"医院列表",doctorAndBigdepAndHostpitals);
    }

    @Override
    public ResponseResult getDoctorByid(Integer id) {
        List<AppDoctor> appdoctors = appDoctorMapper.selectList(null);
        List<DoctorAndBigdepAndHostpital>doctorAndBigdepAndHostpitals=new ArrayList<>();
        for (AppDoctor appDoctor : appdoctors) {
            LambdaQueryWrapper<AppBigdep> lm = new LambdaQueryWrapper<>();
            lm.eq(AppBigdep::getId, appDoctor.getDepartmentId());
            List<AppBigdep> appBigdeps = appBigdepMapper.selectList(lm);
            LambdaQueryWrapper<APPHospital>lms=new LambdaQueryWrapper<>();
            lms.eq(APPHospital::getId,appDoctor.getHospitalId());
            List<APPHospital> appHospitals = appHospitallMapper.selectList(lms);
            DoctorAndBigdepAndHostpital doctorAndBigdepAndHostpital = new DoctorAndBigdepAndHostpital(appDoctor, appBigdeps.get(0), appHospitals.get(0));
            doctorAndBigdepAndHostpitals.add(doctorAndBigdepAndHostpital);
        }
        for (DoctorAndBigdepAndHostpital doctorAndBigdepAndHostpital : doctorAndBigdepAndHostpitals) {
            if (doctorAndBigdepAndHostpital.getAppDoctor().getId()==id){
                return ResponseResult.okResult(200,"查询成功",doctorAndBigdepAndHostpital);
            }
        }
        return null;
    }

    @Override
    public ResponseResult getDoctorinfo(Integer id) {
        List<AppDoctor> appDoctors = appDoctorMapper.selectList(null);
        for (AppDoctor appDoctor : appDoctors) {
            if (appDoctor.getId()==id){
                LambdaQueryWrapper<AppDoctorInfo> lmw = new LambdaQueryWrapper<>();
                lmw.eq(AppDoctorInfo::getBigdepid, appDoctor.getDepartmentId());
                AppDoctorInfo appDoctorInfo = appDoctorInfoMapper.selectOne(lmw);
                return ResponseResult.okResult(200,"查询成功",appDoctorInfo);
            }
       }
        return null;
    }
}
